package liumengbaio.lolzhangyoubao.Activity.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;

import liumengbaio.lolzhangyoubao.Activity.Adapter.ShowPlayItemAdapter;
import liumengbaio.lolzhangyoubao.Activity.InFo.ShowPlayItem;
import liumengbaio.lolzhangyoubao.R;

public class ShowPlayActivity extends AppCompatActivity {
    private ListView show_play_listview;
    private ShowPlayItemAdapter adapter;
    private String url = "http://lol.zhangyoubao.com/apis/rest/Play" +
            "sService/userPlayList?order_kind=0&role_id=0&seaso" +
            "n=8&page=1&i_=O3HIGPgPeO4G8XZ1IQRGtUi/%2B%2B3cVEri" +
            "/rv%2B4zV2Zx4=&deviceId=O3HIGPgPeO4G8XZ1IQRGtUi/%2" +
            "B%2B3cVEri/rv%2B4zV2Zx4=&t_=1473936673172&p_=11861" +
            "&v_=400902&a_=lol&pkg_=com.anzogame.lol&d_=android" +
            "&osv_=17&cha=baiduMartket&u_=&modle_=vivo%20Y15T&a" +
            "pi=PlaysService.userPlayList&time=1473936537&game=" +
            "lol&secretId=AKIDz8krbsJ" +
            "5yKBZQpn74WFkmLPx3gnPhESA&secretVersion=v1.0&nonce" +
            "=372956&secretSignature=5ge63pGowEUETgZjYo3Jmx1FbRI=";
    private ShowPlayItem showPlayItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_play);
        initView();
    }

    private void initView() {
        findViewById(R.id.edit_head_left_title).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.tv_find_friend_publish).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_tv_find_friend_publish=new Intent(ShowPlayActivity.this,SelectHerosActivity.class);
                startActivity(intent_tv_find_friend_publish);
            }
        });
        show_play_listview = (ListView) findViewById(R.id.show_play_listview);
        getDataFromWeb();
    }

    private void getDataFromWeb() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                final Request request = new Request.Builder()
                        .url(url)
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        Toast.makeText(ShowPlayActivity.this, "数据获取错误", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();
                        showPlayItem = gson.fromJson(result, ShowPlayItem.class);
                        if (showPlayItem.getData().size() > 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(0);
                        }
                    }
                });
            }
        }).start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                adapter = new ShowPlayItemAdapter(ShowPlayActivity.this, showPlayItem);
                show_play_listview.setAdapter(adapter);
            } else {
                Toast.makeText(ShowPlayActivity.this, "发生未知错误", Toast.LENGTH_SHORT).show();
            }
        }
    };
}
